#include "crtutil.h"
#include <windows.h>


static bool Initialized = false;


static bool SupportHRPC = false;
// High resolution performance counter - QueryPerformanceFrequency
static double InvFrequency = 0;
static LARGE_INTEGER StartCounter;




void CRTUtilInit()
{
	Initialized = true;

	LARGE_INTEGER Frequency;
	QueryPerformanceFrequency( &Frequency );
	SupportHRPC = (Frequency.LowPart != 0 || Frequency.HighPart != 0);
	
	if (SupportHRPC)
	{
		InvFrequency = 1000.0 / (double)Frequency.QuadPart;
		QueryPerformanceCounter( &StartCounter );
	}
}

double CRTGetCurrentTime()
{
	double CurrentTime = 0;

	if (Initialized)
	{
		if (SupportHRPC)
		{
			LARGE_INTEGER CurrentCounter;

			QueryPerformanceCounter( &CurrentCounter );

			CurrentTime = (double)(CurrentCounter.QuadPart - StartCounter.QuadPart) * InvFrequency;
		}
		else
		{
			CurrentTime = (double)GetTickCount();
		}
	}

	return CurrentTime;
}